package com.java复习.jdbc.游戏.demo3;

import java.util.*;

/**
 * 打游戏
 */
public class Play {

    //输入
    Scanner scanner  = new Scanner(System.in);
    //登录用户
    User user;
    //user表操作
    UserDao userDao = new UserDao();
    ArticleDao articleDao = new ArticleDao();
    ArrayList<User> listMonster;//怪物
    HashMap<Integer,String> initMap;//地图
    ArrayList<Article> listShop;//商店

    /**
     * 登录
     */
     public void login(){
        int num=3;
        while (true){
            if(num!=0){
                System.out.println("请输入账号：");
                String account = scanner.next();
                System.out.println("请输入密码：");
                String pwd = scanner.next();
                //账号密码 查询
                user = userDao.login(account,pwd);
                if(user==null){
                    num--;
                    System.out.println("登录失败，剩余："+num+"次机会");
                }else{
                    System.out.println("登录成功");
                    //调用游戏
                    playGame();

                    return;
                }
            }else{
                System.out.println("系统终止");
                System.exit(0);//系统终止
            }
        }

    }

    /**
     * 注册
     */
     public void register(){
         System.out.println("请输入账号：");
         String account = scanner.next();
         System.out.println("请输入密码：");
         String pwd = scanner.next();
         System.out.println("请输入昵称：");
         String name = scanner.next();
         Object[] objects = {account,pwd,name};
         UserDao.update("insert into user(account,password,name,ATK,DEF,MP,HP) values(?,?,?,100,100,100,100)",objects);
         System.out.println("【注册成功】");
    }

    /**
     * 玩儿游戏
     */
    public void playGame(){
        //初始化怪物
        initMonster();
        //初始化商店
        initShop();
        //初始化地图
        initMap();
        while(true){
            System.out.println("############################\n" +
                    "\t\t##  欢迎"+user.getName()+"来到新手村    ##\n" +
                    "\t\t##输入以下指令开始游戏    ##\n" +
                    "\t\t##1：查看角色信息         ##\n" +
                    "\t\t##2：查看背包信息         ##\n" +
                    "\t\t##3：查看地图             ##\n" +
                    "\t\t##4：查看商店             ##\n" +
                    "\t\t##5：退出游戏             ##\n" +
                    "\t\t##其他：异常提示：指令有误请重新输入 ##\t\n" +
                    "\t\t############################");
            int num = scanner.nextInt();
            switch (num){
                case 1:
                    System.out.println(user);break;
                case 2:
                    selectArticle();break;
                case 3:
                    selectMap();break;
                case 4:show();break;
                case 5:
                    System.out.println("退出游戏");System.exit(0);
            }
        }


    }

    /**
     * 查看商店+购买物品
     */
    public void show(){
       while(true){
           System.out.println("【商店】：老板快来挑选！！！！");
           for (Article article:listShop) {
               System.out.println(article);
           }
           System.out.println("是否购买商品：1、购买 2、退出商店");
           int num =  scanner.nextInt();
           switch (num){
               case 1:buy();break;
               case 2:return;
           }
       }
    }

    /**
     * 卖商品
     */
    public void buy(){
        System.out.println("请输入购买的物品编号：");
        int num = scanner.nextInt();
        System.out.println("请输入购买的物品数量：");
        int num1 = scanner.nextInt();
        for (Article article:listShop) {
            if(article.getId()==num){
                if(user.getMoney()<article.getPrice()*num1){
                    System.out.println("金额不足");
                    return;
                }else{
                    //存入数据库
                    user.setMoney(user.getMoney()-article.getPrice()*num1);
                    //修改用户
                    Object[] objects = {user.getMoney(),user.getId()};
                    UserDao.update("update user set money=? where id=?",objects);

                    //存物品
                    Article article1 = ArticleDao.select(article.getId());

                    if(article1==null){
                        Object[] objects1 = {article.getName(),article.getATK()
                        ,article.getDEF(),article.getMP(),article.getHP(),num1,article.getPrice(),user.getId()};
                        ArticleDao.update("insert into article(name,ATK,DEF,MP,HP,num,price,user_id) values(?,?,?,?,?,?,?,?)",objects1);

                    }else if(article1.getNum()<=0){
                        Object[] objects1 = {user.getId()};
                        ArticleDao.update("delete from article where user_id=?",objects1);
                    }else {
                        Object[] objects1 = {article1.getNum()+num1,user.getId()};
                        ArticleDao.update("update article set num=? where user_id=?",objects1);
                    }
                    System.out.println("购买成功，余额："+user.getMoney());
                }

            }
        }
    }

    /**
     * 查看地图+随机遇怪
     */
    public void selectMap(){

       while(true){
           Set<Integer> keySet = initMap.keySet();
           for (Integer key:
                   keySet) {
               System.out.println("【地图编号:"+key+"】");
               System.out.println(initMap.get(key));
           }
           System.out.println("【请输入地图编号（输入0，返回新手村）：】");
           int num = scanner.nextInt();
           switch (num){
               case 0:return;
               case 1:
                   Random random = new Random();
                   int num1 = random.nextInt(3)+1;
                   int hp = user.play(listMonster.get(num1));
                   if(hp==0){
                       break;
                   }else{
                       return;
                   }
               case 2:
                   Random random1 = new Random();
                   int num2 = random1.nextInt(1)+4;
                   int hp1 =user.play(listMonster.get(num2));
                   if(hp1==0){
                       break;
                   }else{
                       return;
                   }
           }
       }

    }




    /**
     * 查看背包
     */
    public void selectArticle(){
        ArrayList<Article>  lists = articleDao.selectid(user.getId());
        for (Article article: lists) {
            //删除数量为0的物品
            if(article.getNum()<=0){
                Object[] bbj = {article.getId()};
                ArticleDao.update("delete from article where id=?",bbj);
            }else {
                System.out.println("【背包数据】");
                System.out.println(article);
            }
        }
        System.out.println("是否使用物品：【1、使用 2、退出背包】");
        int num = scanner.nextInt();
        switch (num){
            case 1:playGoods(lists);break;
            case 2:return;
        }
    }
    /**
     * 使用物品
     */
    public void playGoods(ArrayList<Article> lists){
        System.out.println("请输入物品编号");
        int id = scanner.nextInt();
        for (Article article: lists) {
            if(id== article.getId()){
                article.play(user);
                //跟新数据库
                Object[] objects = {article.getNum(),user.getId()};
                ArticleDao.update("update article set num=? where user_id=?",objects);
                System.out.println("使用成功");
                System.out.println("【人物数据】");
                System.out.println(user);
                selectArticle();
                return;
            }
        }

    }



    /**
     * 初始化妖怪
     */
    public void initMonster(){
        listMonster = new ArrayList<User>();
        listMonster.add(new User(1,"黑山老妖",null,null,1000,1000,1000,1000,10000));
        listMonster.add(new User(2,"野狗",null,null,19,100,100,1000,100));
        listMonster.add(new User(3,"蜘蛛",null,null,50,80,100,100,100));
        listMonster.add(new User(4,"神兽",null,null,1000,1000,1000,1000,10000));
        listMonster.add(new User(5,"野鸡",null,null,80,80,100,100,100));
    }

    /**
     * 初始化地图
     */
    public void initMap(){
        initMap = new HashMap<Integer,String>();
        initMap.put(1,"【黑风洞】：阴暗潮湿之地，散发着........盛产：野狗、蜘蛛、黑山老妖");
        initMap.put(2,"【神山】：阴暗潮湿之地，散发着........盛产：野鸡、神兽");
    }

    /**
     * 初始化商店
     */
    public void initShop(){
        listShop = new ArrayList<Article>();
        listShop.add(new Article(1,"破旧的短剑",10,0,0,0,9999,1));
        listShop.add(new Article(2,"破旧的衣服",0,10,0,0,9999,1));
        listShop.add(new Article(3,"小红瓶",0,0,0,10,9999,1));
        listShop.add(new Article(4,"小蓝瓶",0,0,10,0,9999,1));
    }


}
